﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

using XmlVisualizer.Dialogs;

namespace XmlVisualizer.Controls {

	/// <summary>
	/// List of attribute, inherits from ObservableCollection&lt;XAttribute&gt;.
	/// </summary>
	public class AttributesList : ObservableCollection<XAttribute> {
		#region Methods
		/// <summary>
		/// Removes attribute with specified name.
		/// </summary>
		/// <param name="name">Name of the attribute to remove.</param>
		public void Remove(XName name) {
			foreach (XAttribute att in this) {
				if (att.Name == name) {
					this.Remove(att);
					break;
				}
			}
		}

		/// <summary>
		/// Sets attribute value. Either creates new attribute or changes
		/// old value.
		/// </summary>
		/// <param name="name">Name of the attribute.</param>
		/// <param name="value">Attribute's value.</param>
		public void SetValue(XName name, object value) {
			foreach (XAttribute att in this) {
				if (att.Name == name) {
					att.SetValue(value);
					return;
				}
			}
			this.Add(new XAttribute(name, value));
		}
		#endregion
		
		protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
			base.OnCollectionChanged(e);
			if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) {
				//foreach (MyXAttribute att in e.NewItems) {
				//    att.PropertyChanged += new PropertyChangedEventHandler(att_PropertyChanged);
				//}
			} else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) {
				//foreach (MyXAttribute att in e.OldItems) {
				//    att.PropertyChanged -= new PropertyChangedEventHandler(att_PropertyChanged);
				//}
			}
		}
		
	}

	/// <summary>
	/// Attributes user control.
	/// </summary>
	public partial class ucAttributes : UserControl {
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the ucAttributes class.
		/// </summary>
		public ucAttributes() {
			InitializeComponent();
		}

		/// <summary>
		/// Initializes the ucAttributes class.
		/// </summary>
		static ucAttributes() {
			FrameworkPropertyMetadata attributesMetadata = new FrameworkPropertyMetadata(null,
				FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
			AttributesProperty = DependencyProperty.Register("Attributes", typeof(AttributesList), typeof(ucAttributes), attributesMetadata);

			FrameworkPropertyMetadata elementNameMetadata = new FrameworkPropertyMetadata(null,
				FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault);
			ElementNameProperty = DependencyProperty.Register("ElementName", typeof(XName), typeof(ucAttributes), elementNameMetadata);
		}
		#endregion

		#region Dependency properties
		/// <summary>
		/// Identifies the AttributesProperty dependency property.
		/// </summary>
		public static DependencyProperty AttributesProperty;

		/// <summary>
		/// Gets or sets the AttributesProperty.
		/// </summary>
		public AttributesList Attributes {
			get { return (AttributesList)GetValue(AttributesProperty); }
			set { SetValue(AttributesProperty, value); }
		}

		/// <summary>
		/// Identifies the ElementNameProperty.
		/// </summary>
		public static DependencyProperty ElementNameProperty;

		/// <summary>
		/// Gets or sets the ElementNameProperty.
		/// </summary>
		public XName ElementName {
			get { return (XName)GetValue(ElementNameProperty); }
			set { SetValue(ElementNameProperty, value); }
		}
		#endregion

		#region Event handlers
		private void lwAttributes_Click(object sender, RoutedEventArgs e) {
			if (e.OriginalSource is Button) {
				Button btn = e.OriginalSource as Button;
				XName name = btn.Tag as XName;
				Attributes.Remove(name);
			}
		}

		private void btn_AddAttribute_Click(object sender, RoutedEventArgs e) {
			DialogAddElementAttribute addAttributeWindow = new DialogAddElementAttribute(Attributes, ElementName);
			Point pos = Mouse.GetPosition(this);
			Point pos2 = PointToScreen(pos);
			addAttributeWindow.Left = pos2.X;
			addAttributeWindow.Top = pos2.Y;
			addAttributeWindow.ShowModal();
		}
		#endregion
	}
}
